Ext.namespace("yft.posPurchaseOrder");
var posPurchaseOrder_authArray_1;
var posPurchaseOrderHeaderStore;
var posPurchaseOrderDetailJsonStore;
function getPosPurchaseOrderSearch_1(){
	
}
yft.posPurchaseOrder.posPurchaseOrderPanel = Ext.extend(Ext.Panel, {
	authArray : '',
	closable : true,
	autoScroll : true,
	layout : "fit",
	bodyStyle: my_scroll_cs,
	initComponent : function() {
		yft.posPurchaseOrder.posPurchaseOrderPanel.superclass.initComponent.call(this);
		
		posPurchaseOrder_authArray_1 =  this.authArray;

        posPurchaseOrderHeaderStore = new Ext.data.JsonStore({
            url:basepath+'/yft/posPurchaseOrder/getPosPurchaseOrderHeaderList.html',
            root:'root',
            totalProperty:'totalProperty',
            fields:[
                {name: 'id'},
                {name: 'purchaseOrderNo'},
                {name: 'status'},
                {name: 'statusDesc'},
                {name: 'channelName'},
                {name: 'expressNo'}
            ]
        });
        posPurchaseOrderHeaderStore.load({params:{start: 0,limit: colNum1}});

        var col ='';
        col =[
            {header:'NO',align:'center',width:30,renderer:function(value, cellmeta, record, rowIndex){return rowIndex + 1;}}
            ,{header:'采购订单号',dataIndex:'purchaseOrderNo',align:"center",sortable:true}
            ,{header:'进货渠道',dataIndex:'channelName',align:"center",sortable:true}
            ,{header:'订单状态',dataIndex:'statusDesc',align:"center",sortable:true}
            ,{header:'快递单号',dataIndex:'expressNo',align:"center",sortable:true}
        ];
        var toolBar = [];
//        if(getCompetenceForAllDev(qualityAdjustReceipt_authArray_1,authId[850])){
            toolBar.push('-');
            toolBar.push({
                iconCls : 'file-add',
                text : '新建',
                iconCls : 'add',
                pressed : false,
                handler : function(){
                    var p = main.getComponent("newTab_posPurchaseOrderAdd");
                    if(p){
                        main.setActiveTab(p);
                        main.doLayout();
                    }else{
                        Ext.Ajax.request({
                            url : basepath+'/yft/posPurchaseOrder/clearSessionPurchaseOrderItem.html',
                            callback:function(options,success,response){
                                showNewTabPanel('采购单新增','posPurchaseOrderAdd',yft.posPurchaseOrder_form.posPurchaseOrderForm);
                            }
                        });
                    }
                },
                scope : this
            });
            toolBar.push('-');
//        }
        toolBar.push(new Ext.Toolbar.Fill());
        toolBar.push('存货编号:&nbsp;');
        toolBar.push({
            xtype : 'textfield',
            id : 'purchaseOrderNo',
            width : 90
        });
        toolBar.push('&nbsp;','-');
        toolBar.push({
            text : '查询',
            iconCls : 'search',
            pressed : false,
            handler : function(){
                var purchaseOrderNo = Ext.getCmp('purchaseOrderNo').getValue();
                if(!Ext.isEmpty(purchaseOrderNo)){
                    posPurchaseOrderHeaderStore.baseParams['purchaseOrderNo']=purchaseOrderNo;
                }
                posPurchaseOrderHeaderStore.load({params:{start: 0,limit: colNum1}});
            },
            scope : this
        });
        toolBar.push('-');
        toolBar.push({
            text : '取消查询',
            iconCls : 'clear',
            pressed : false,
            handler : function(){
                Ext.getCmp('purchaseOrderNo').setValue('');
                posPurchaseOrderHeaderStore.baseParams['purchaseOrderNo']='';
            },
            scope : this
        });
        var toolBarReal = new Ext.Toolbar({
            style: topBarStyle,
            items: toolBar
        });

        var mrMenu = [];
        /*
         * if(getCompetenceForAllDev(posPurchaseOrder_authArray_1,authId[809])){
         mrMenu.push({
         text:"查看明细",
         iconCls : 'detail',
         handler: function(grid ,rowIndex,cellIndex,e){
         var record = grid.getStore().getAt(rowIndex);
         }
         });
         }*/
//        if(getCompetenceForAllDev(posPurchaseOrder_authArray_1,authId[809])){
            mrMenu.push({
                text:"修改订单",
                iconCls : 'modify',
                canShowMenuFun:function(grid ,rowIndex,cellIndex,e){
                    var record = grid.getStore().getAt(rowIndex);
                    if(record.data.status!='NEW')
                        return false;
                    else
                        return true;
                },
                handler: function(grid ,rowIndex,cellIndex,e){
                    var record = grid.getStore().getAt(rowIndex);
                    var p = main.getComponent("newTab_purchaseOrderUpdate");
                    if(p){
                        Ext.MessageBox.alert("提示","您已打开一个订单修改页签，请关闭或完成修改后重试！",function(o){
                            main.setActiveTab(p);main.doLayout();
                        })
                    }else{
                        showProgress();
                        Ext.Ajax.request({
                            url : basepath+'/yft/posPurchaseOrder/getPurchaseOrderForUpdate.html',
                            params : {
                                headerId : record.data.id
                            },
                            callback:function(options,success,response){
                                var m =Ext.decode(response.responseText);
                                mainMessageBox.hide();
                                if(m.success){
                                    showNewTabPanel('采购单修改('+record.data.purchaseOrderNo+')','purchaseOrderUpdate',yft.posPurchaseOrder_form.posPurchaseOrderForm,m.root);
                                }else{
                                    Ext.Msg.alert("提示", "对不起,系统异常,请稍候再试!");
                                }
                            }
                        });
                    }
                }
            });
//        }
//        if(getCompetenceForAllDev(posPurchaseOrder_authArray_1,authId[809])){
            mrMenu.push({
                text:"订单审核",
                iconCls : 'confirm',
                canShowMenuFun:function(grid ,rowIndex,cellIndex,e){
                    var record = grid.getStore().getAt(rowIndex);
                    if(record.data.status!='NEW')
                        return false;
                    else
                        return true;
                },
                handler: function(grid ,rowIndex,cellIndex,e){
                    var record = grid.getStore().getAt(rowIndex);
                    Ext.MessageBox.confirm("提示","您确认要审核当前选中的订单吗?",function(o){
                        if(o =='yes'){
                            if(main.getComponent("newTab_purchaseOrderUpdate"))
                                main.remove(main.getComponent("newTab_purchaseOrderUpdate"));
                            showProgress();
                            Ext.Ajax.request({
                                url : basepath+'/yft/posPurchaseOrder/auditPosPurchaseOrder.html',
                                params : {
                                    headerId : record.data.id,
                                    channelName : record.data.channelName
                                },
                                callback:function(options,success,response){
                                    var m =Ext.decode(response.responseText);
                                    mainMessageBox.hide();
                                    if(m.success){
                                        Ext.Msg.alert("提示", "操作成功！",function(){
                                            posPurchaseOrderHeaderStore.reload();
                                        });
                                    }else{
                                        var ms = "对不起,系统异常,请稍候再试!";
                                        if(m.msg!=null)
                                            ms = m.msg;
                                        Ext.Msg.alert("提示", ms,function(){
                                            posPurchaseOrderHeaderStore.reload();
                                        });
                                    }
                                }
                            });
                        }
                    });
                }
            });
//        }
//        if(getCompetenceForAllDev(posPurchaseOrder_authArray_1,authId[809])){
            mrMenu.push({
                text:"订单删除",
                iconCls : 'confirm',
                canShowMenuFun:function(grid ,rowIndex,cellIndex,e){
                    var record = grid.getStore().getAt(rowIndex);
                    if(record.data.status!='NEW')
                        return false;
                    else
                        return true;
                },
                handler: function(grid ,rowIndex,cellIndex,e){
                    var record = grid.getStore().getAt(rowIndex);
                    Ext.MessageBox.confirm("提示","您确认要删除当前选中的订单吗?",function(o){
                        if(o =='yes'){
                            if(main.getComponent("newTab_purchaseOrderUpdate"))
                                main.remove(main.getComponent("newTab_purchaseOrderUpdate"));
                            showProgress();
                            Ext.Ajax.request({
                                url : basepath+'/yft/posPurchaseOrder/delPosPurchaseOrder.html',
                                params : {
                                    headerId : record.data.id
                                },
                                callback:function(options,success,response){
                                    var m =Ext.decode(response.responseText);
                                    mainMessageBox.hide();
                                    if(m.success){
                                        Ext.Msg.alert("提示", "操作成功！",function(){
                                            posPurchaseOrderHeaderStore.reload();
                                        });
                                    }else{
                                        var ms = "对不起,系统异常,请稍候再试!";
                                        if(m.msg!=null)
                                            ms = m.msg;
                                        Ext.Msg.alert("提示", ms,function(){
                                            posPurchaseOrderHeaderStore.reload();
                                        });
                                    }
                                }
                            });
                        }
                    });
                }
            });
//        }
        
        var posPurchaseOrderHeaderGrid = new Ext.grid.GridPanel({
            autoScroll:true,
            id:'posPurchaseOrderHeaderGrid_id',
            loadMask: new Ext.LoadMask(Ext.getBody(), {msg:"数据加载中..."}),
            columns: col,
            myRightclickMenu: mrMenu,
            keys:[{
                key: Ext.EventObject.ENTER,
                fn: function(){
                },
                scope: this
            }],
            stripeRows : true,
            tbar : toolBarReal,
            bodyStyle:panelMainStyle,
            bbar:new Ext.PagingToolbar({
                style: bottomBarStyle_1,
                pageSize: colNum1,
                store: posPurchaseOrderHeaderStore,
                displayInfo: true,
                displayMsg:displayMsgOfBbar,
                emptyMsg: emptyMsgOfBbar
            }),
            store: posPurchaseOrderHeaderStore
        });

         posPurchaseOrderDetailJsonStore = new Ext.data.JsonStore({
            url: basepath + '/yft/posPurchaseOrder/getPosPurchaseOrderDetailList.html',
            root:'root',
            totalProperty:'totalProperty',
            fields:[
                {name: 'id'},
                {name: 'machineSnCode'},
                {name: 'terminalSn'},
                {name: 'passwordKeySn'},
//                {name: 'simSn'},
                {name: 'pattern'},
                {name: 'modelName'},
                {name: 'quality'}
            ]
        });

        var posPurchaseOrderDetailCol = [
            {header:'NO',align:'center',width:30,renderer:function(value, cellmeta, record, rowIndex){return rowIndex + 1;}}
            ,{header:'终端序列号',dataIndex:'machineSnCode',align:"center",sortable:true}
            ,{header:'终端号',dataIndex:'terminalSn',align:"center",sortable:true}
            ,{header:'密码键盘号',dataIndex:'passwordKeySn',align:"center",sortable:true}
//            ,{header:'sim卡编号',dataIndex:'simSn',align:"center",sortable:true}
            ,{header:'机型',dataIndex:'pattern',align:"center",sortable:true}
            ,{header:'型号',dataIndex:'modelName',align:"center",sortable:true}
            ,{header:'数量',dataIndex:'quality',align:"center",sortable:true}
        ];

        var posPurchaseOrderDetailGrid = new Ext.grid.GridPanel({
            enableHdMenu : false,
            id:"posPurchaseOrderDetailGrid_id",
            enableColumnMove :false,
            enableColumnResize :false,
            enableDragDrop :false,
            draggable :false,
            bodyStyle:panelMainStyle,
            autoScroll: false,
            loadMask: new Ext.LoadMask(Ext.getBody(), {msg:"数据加载中..."}),
//			autoExpandColumn: 2,
            columns: posPurchaseOrderDetailCol,
            stripeRows : true,
            bbar:new Ext.PagingToolbar({
                style: bottomBarStyle,
                pageSize: colNum1,
                store: posPurchaseOrderDetailJsonStore,
                displayInfo: true,
                displayMsg:displayMsgOfBbar,
                emptyMsg: emptyMsgOfBbar
            }),
            store: posPurchaseOrderDetailJsonStore
        });


        var csId_1;
        var selectionModel = Ext.getCmp('posPurchaseOrderHeaderGrid_id').getSelectionModel();
        posPurchaseOrderHeaderStore.on('load',function(store,records,options){
            if(!Ext.isEmpty(csId_1)){
                selectionModel.selectRow(store.find('id',csId_1));
            }
        },this,{delay:1000})
        posPurchaseOrderHeaderGrid.on('rowclick',function(grid , rowIndex, e){
            var record = grid.getStore().getAt(rowIndex);
            csId_1 = record.data.csId;
            posPurchaseOrderDetailJsonStore.removeAll();
            posPurchaseOrderDetailJsonStore.baseParams['headerId'] = record.data.id;
            posPurchaseOrderDetailJsonStore.load({params:{start: 0,limit: colNum2_b}});

        });
        var baseInfoMainPanel = new Ext.Panel({
            autoScroll: false,
            bodyStyle:panelMainStyle,
//			ctCls:'border:0px solid #D9E8FB;',
//			style:'position:absolute;left:-1px;bottom:-1px;',
//			frame:true,
//			style:'background:red;',
            layout : 'table',
            layoutConfig: {
                columns: 1
            },
            items: [posPurchaseOrderHeaderGrid,posPurchaseOrderDetailGrid]
        });
        baseInfoMainPanel.on('resize',function(){
            posPurchaseOrderHeaderGrid.setWidth(baseInfoMainPanel.getInnerWidth());
            posPurchaseOrderHeaderGrid.setHeight(baseInfoMainPanel.getInnerHeight()*0.5);
            posPurchaseOrderDetailGrid.setWidth(baseInfoMainPanel.getInnerWidth());
            posPurchaseOrderDetailGrid.setHeight(baseInfoMainPanel.getInnerHeight()*0.5);
        });
        baseInfoMainPanel.on('afterlayout',function(){
            if(baseInfoMainPanel.getInnerWidth()>0){
                posPurchaseOrderHeaderGrid.setWidth(baseInfoMainPanel.getInnerWidth());
                posPurchaseOrderHeaderGrid.setHeight(baseInfoMainPanel.getInnerHeight()*0.5);
                posPurchaseOrderDetailGrid.setWidth(baseInfoMainPanel.getInnerWidth());
                posPurchaseOrderDetailGrid.setHeight(baseInfoMainPanel.getInnerHeight()*0.5);
            }
        });
        this.add(baseInfoMainPanel);
    }
});
